//
//  STMGetCaptchaViewController.m
//  StoreManagement
//
//  Created by centling on 16/6/1.
//  Copyright © 2016年 alfred. All rights reserved.
//

#import "STMGetCaptchaViewController.h"
#import "STMConfirmCaptchaViewController.h"

@interface STMGetCaptchaViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *backButton;
@property (weak, nonatomic) IBOutlet UITextField *idTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UIButton *getCaptchaButton;

@end

@implementation STMGetCaptchaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = MAINGRAYCOLOR;
    [self.backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.getCaptchaButton addTarget:self action:@selector(getCaptchaButtonClick) forControlEvents:UIControlEventTouchUpInside];
    self.phoneTextField.delegate = self;
}

- (void)getCaptchaButtonClick {
    if ([self.idTextField.text isEqualToString:@""]) {
        [GlobalUtil HUDShowMessage:@"请输入账号" addedToView:self.view];
        return;
    }
    if ([self.phoneTextField.text isEqualToString:@""]) {
        [GlobalUtil HUDShowMessage:@"请输入手机号" addedToView:self.view];
        return;
    }
    if (![GlobalUtil validateMobile:self.phoneTextField.text]) {
        [GlobalUtil HUDShowMessage:@"请输入合法的手机号" addedToView:self.view];
        return;
    }
    [SVProgressHUD showWithStatus:@"加载中..."];
    [STMNetworkAPI getCaptchaWithSellerName:self.idTextField.text
                                     mobile:self.phoneTextField.text
                                    success:^(STMGetCaptchaModel *result) {
                                        [SVProgressHUD dismiss];
                                        if ([result.code isEqualToString:@"200"]) {
                                            [UserDefaultHandler saveUserDetault:result.result.captcha key:kMobileCaptcha];
                                            [UserDefaultHandler saveUserDetault:self.idTextField.text key:kSellerName];
                                            STMConfirmCaptchaViewController *confirmCaptchaVC = [[STMConfirmCaptchaViewController alloc] init];
                                            [self presentViewController:confirmCaptchaVC animated:YES completion:^{
                                                [GlobalUtil HUDShowMessage:@"验证码已发送" addedToView:confirmCaptchaVC.view];
                                            }];
                                        } else {
                                            [GlobalUtil HUDShowMessage:result.message addedToView:self.view];
                                        }
                                    }
                                    failure:^(NSError *err) {
                                        [SVProgressHUD dismiss];
                                        [GlobalUtil HUDShowMessage:@"请检查网络连接" addedToView:self.view];
                                    }];
}


- (void)backButtonClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark --UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([self.phoneTextField isFirstResponder]) {
        if (string.length - range.length + textField.text.length > 11) {
            return NO;
        }
        //删除键可用
        if([string isEqualToString:@""]){
            return YES;
        }
        //只输入数字
        return [GlobalUtil validateNumOnly:string digits:11];
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
